﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Web;

namespace App.Sdx.Api
{
    public class LogHelper
    {
        /// <summary>
        /// 日志根目录
        /// </summary>
        private static string LogRoot
        {
            get
            {
                return HttpRuntime.AppDomainAppPath + "\\Logs";
            }
        }

        public static void DoDataWrite(string msg)
        {
            try
            {
                var strLogPath = LogRoot + "\\WebServiesData\\";
                if (!(Directory.Exists(strLogPath)))
                {
                    Directory.CreateDirectory(strLogPath);
                }
                strLogPath = strLogPath + DateTime.Now.ToString("yyyyMMdd") + ".log";
                TextWriter tw = new StreamWriter(strLogPath, true);
                tw.WriteLine(msg);
                tw.Close();
            }
            catch (Exception ex)
            {
                DoErrorWrite(ex.ToString());
            }
        }

        public static void DoErrorWrite(string msg)
        {
            try
            {
                var strLogPath = LogRoot + "\\Errors\\";
                if (!(Directory.Exists(strLogPath)))
                {
                    Directory.CreateDirectory(strLogPath);
                }
                strLogPath = strLogPath + DateTime.Now.ToString("yyyyMMdd") + ".log";
                TextWriter tw = new StreamWriter(strLogPath, true);
                tw.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\t" + msg);
                tw.Close();
            }
            catch (Exception ex)
            {
                DoErrorWrite(ex.ToString());
            }
        }
    }
}